[contents] [prev] [next] [top] [bottom] (5 out of 6)

Threads

Threads are independent processes that run in parallel with one another, sharing processor time. For example, you could process a long search or compilation in the background in one thread, and run a continuous animation in another thread, while still interacting with the main application. A ScriptX title starts running in the main thread, which can be referenced through the global variable theMainThread. The title can create and run other threads at any time.

The ScriptX thread system is described in detail in the ScriptX Components Guide. The ScriptX language provides a shorthand construct for spawning threads, similar to that used in the UNIX shell languages. The ampersand character ( & ), when used at the end of an expression as a postfix operator, starts that expression running in a separate thread.

(p := importBitmap "monalisa") &
computePrimesTo 10000 &

The example above starts threads running functions called importBitmap and computePrimesTo in parallel with the main program. Any number of separate threads can run at the same time, subject to the limitations of memory and processor time, allowing parallel processing of different tasks.

The thread operator ( & ) creates a thread object, with the expression that precedes it as the code that runs in the thread. The thread is immediately active. You can test the status of the thread or control the thread by using any of the global functions or generic functions described in the ScriptX Class Reference.

t := computePrimesTo 100 &
repeat until (t.status = @done) do
	print "tick"
print "computation completed"

This example executes a computePrimesTo function as a thread and then prints "tick" to the debug stream until the status of the thread changes from @running to @done.

The & operator is a shorthand form for the callInThread global function, which creates a new RegularThread object with an operating priority of @normal. The value of priority, either @normal or @high, determines how much processing time the thread receives. See the "Threads" chapter of the ScriptX Components Guide for more details on thread priority.


This document is part of the ScriptX Language Guide, one of the volumes of the ScriptX Technical Reference Series. ScriptX is developed by the ScriptX Engineering Team at Apple Computer, successor to the Kaleida Engineering Team at Kaleida Labs, Inc.

Copyright 1996 Apple Computer, Inc. All Rights Reserved.